<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="apple-touch-fullscreen" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="x5-fullscreen" content="true" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="/img/favicon/favicon.ico" />
  <title>Box 盒子|Mars3D|三维地图| 火星科技|合肥火星科技|合肥火星科技有限公司</title>

  <script type="text/javascript" src="/lib/include-lib.js" libpath="/lib/"
    include="jquery,layer,toastr,font-awesome,bootstrap,bootstrap-table,bootstrap-checkbox,bootstrap-slider,layer,haoutil,ztree,mars3d,shpGeojson,es5-widget"></script>

  <link href="/css/style.css" rel="stylesheet" />

  <script src="./Shp2JsonLayer.js" type="text/javascript"></script>
</head>

<body class="dark">
  <div id="mars3dContainer" class="mars3d-container"></div>

  <div class="infoview">
    <div>
      <input id="shoXZM" type="button" class="btn btn-primary" value="乡镇面" />
      <input id="shoGCD" type="button" class="btn btn-primary" value="高程点" />
    </div>

    <div>
      图层状态：
      <div class="checkbox checkbox-primary checkbox-inline">
        <input id="enabledShowHide" class="styled" type="checkbox" checked />
        <label for="enabledShowHide"> 显示 </label>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="enabledPopup" class="styled" type="checkbox" checked />
          <label for="enabledPopup"> Popup </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="enabledTooltip" class="styled" type="checkbox" />
          <label for="enabledTooltip"> Tooltip </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="enabledRightMenu" class="styled" type="checkbox" />
          <label for="enabledRightMenu"> 右键菜单 </label>
        </div>
        <div id="enabledCluster" class="checkbox checkbox-primary checkbox-inline" style="display: table;margin-left: 74px">
          <input id="isCluster" class="styled" type="checkbox" />
          <label for="isCluster"> 是否聚合 </label>
        </div>
      </div>
    </div>
  </div>

  <div class="infoview" style="overflow: auto; right: 5px; left: auto; top: 80px; max-height: 88%">
    <ul id="treeOverlays" class="ztree"></ul>
  </div>

  <script src="/js/common.js"></script>
  <script src="./map.js"></script>
  <script src="/js/graphic_layer_state.js"></script>
  <script type="text/javascript">
    "use script" //开发环境建议开启严格模式

    let autoGroup = "SSJD"

    function initUI(options) {
      bindAttributePannel()

      setTimeout(() => {
        if (graphicLayer) {
          $("#enabledShowHide").attr("checked", graphicLayer.show)
          $("#enabledPopup").attr("checked", graphicLayer.hasPopup())
          $("#enabledTooltip").attr("checked", graphicLayer.hasTooltip())
          $("#enabledRightMenu").attr("checked", graphicLayer.hasContextMenu())

          const graphics = graphicLayer.getGraphics()
          if (graphics.length > 0) {
            if (!graphics[0].hasOpacity) {
              $("#enabledOpacity").hide()
            }
            if (!graphics[0].hasEdit) {
              $("#enabledEdit").hide()
            }
            if (!graphics[0].hasCluster) {
              $("#enabledCluster").hide()
            }
          }
        }
      }, 500)

      // // 图层状态
      $("#enabledShowHide").change(function () {
        let val = $(this).is(":checked")
        graphicLayer.show = val
      })

      // 图层交互
      $("#enabledPopup").change(function () {
        let val = $(this).is(":checked")

        if (val) {
          bindLayerPopup2() //map.js中不绑定Popup,可以使用graphic_layer_state.js中的bindLayerPopup2
        } else {
          graphicLayer.unbindPopup()
        }
      })
      $("#enabledTooltip").change(function () {
        let val = $(this).is(":checked")

        if (val) {
          graphicLayer.bindTooltip(
            function (event) {
              const attr = getAttrForEvent(event)
              attr["类型"] = event.graphic?.type
              attr["来源"] = "我是layer上绑定的Toolip"
              attr["备注"] = "我支持鼠标移入交互"

              return mars3d.Util.getTemplateHtml({ title: "矢量图层", template: "all", attr: attr })
            },
            { pointerEvents: true }
          )
        } else {
          graphicLayer.unbindTooltip()
        }
      })
      $("#enabledRightMenu").change(function () {
        let val = $(this).is(":checked")

        if (val) {
          bindLayerContextMenu2() //map.js中不绑定ContextMenu,可以使用graphic_layer_state.js中的bindLayerContextMenu2
        } else {
          graphicLayer.unbindContextMenu(true)
        }
      })
      $("#isCluster").change(function () {
        let val = $(this).is(":checked")
        graphicLayer.clusterEnabled = val
      })

      // 数据维护
      $("#hasEdit").change(function () {
        let val = $(this).is(":checked")
        graphicLayer.hasEdit = val

        // 编辑时，为了方便操作自动关闭Popup，真实项目中请按需修改
        $("#enabledPopup").attr("checked", !val)
        $("#enabledPopup").change()
      })

      $("#shoXZM").click(function () {
        autoGroup = "SSJD"
        shoXZM()
      })

      $("#shoGCD").click(function () {
        autoGroup = function (item) {
          const val = item.attr?.DEM
          if (val < 100) {
            return "<100"
          } else if (val >= 100 && val < 200) {
            return "100~200"
          } else if (val >= 200 && val < 300) {
            return "200~300"
          } else {
            return "300~999"
          }
        }

        shoGCD()
      })
    }

    //===========================树控件处理============================
    treeEvent.on("refTree", (e) => {
      //初始化树
      let setting = {
        check: {
          enable: true
        },
        data: {
          simpleData: {
            enable: true
          }
        },
        callback: {
          onCheck: treeOverlays_onCheck,
          onClick: treeOverlays_onClick
        }
      }

      // 重置上一次的树状数据
      const showIds = [] // 是显示状态的图层id集合
      const openIds = [] // 展开的树节点id集合（如果不想展开，对应图层配置open:false）
      const result = getGraphicsTree({
        forEach: function (item) {
          item.key = item.id // 树控件api需要的唯一标识
          item.title = item.name || "未命名" // 树控件api需要的显示文本字段

          item.checked = true
          if (item.group && item.open !== false) {
            item.open = true
          }
        },
        autoGroup: autoGroup
      })
      console.log("获取到的graphics树", result)

      // 赋予树控件
      $.fn.zTree.destroy()
      $.fn.zTree.init($("#treeOverlays"), setting, result.tree)
    })

    function treeOverlays_onCheck(e, treeId) {
      let zTree = $.fn.zTree.getZTreeObj(treeId)

      //获得所有改变check状态的节点
      let changedNodes = zTree.getChangeCheckedNodes()
      if (changedNodes) {
        for (let i = 0; i < changedNodes.length; i++) {
          let treeNode = changedNodes[i]
          treeNode.checkedOld = treeNode.checked

          let graphic = getGraphicById(treeNode.id)
          if (graphic == null) {
            continue
          }
          let show = treeNode.checked
          //处理图层显示隐藏
          graphic.show = show
        }
      }
    }

    function treeOverlays_onClick(event, treeId, treeNode) {
      let graphic = getGraphicById(treeNode.id)
      if (graphic == null) {
        return
      }
      graphic.flyTo()
    }
  </script>
</body>

</html>
